package com.ruoyi.oto.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.CompanyTagApplyStatus;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.CompanyTags;
import com.ruoyi.oto.domain.CompanyTagsCore;
import com.ruoyi.oto.domain.dto.CompanyTagUpdateDTO;
import com.ruoyi.oto.domain.vo.CompanyTagCoreQueryParamVO;
import com.ruoyi.oto.service.BranchLinkService;
import com.ruoyi.oto.service.CompanyCoreService;
import com.ruoyi.oto.service.CompanyTagsCoreService;
import com.ruoyi.oto.service.CompanyTagsService;
import com.ruoyi.oto.tagvisible.TagVisibleRange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 小程序-企业标签管理
 */
@RestController
@RequestMapping("/api/v2/tag")
public class CompanyTagApiController extends BaseController {

    /**
     * 企业标签服务
     */
    @Autowired
    private CompanyTagsService companyTagsService;

    /**
     * 标签实体服务
     */
    @Autowired
    private CompanyTagsCoreService companyTagsCoreService;
    /**
     *
     */
    @Autowired
    private BranchLinkService branchLinkService;
    /**
     * 企业服务
     */
    @Autowired
    private CompanyCoreService companyCoreService;

    /**
     * 获取企业的标签列表
     * 当companyId为-1的时候代表获取自己的企业的标签
     * @return
     */
   @GetMapping("companyTagList")
   public AjaxResult<List<CompanyTags>> companyTagList(@PathParam("companyId") Long companyId) {
        MemberUser member = this.getLoginUser().getMember();
        CompanyCore companyCore = null;
        if(companyId==-1L){
            //说明是企业用户要查看自己管理的企业的标签
            companyCore = companyCoreService.getOne(new LambdaQueryWrapper<CompanyCore>().eq(CompanyCore::getMemberId, member.getId()));
            if(ObjUtil.isNull(companyCore)){
                throw new ServiceException("您还没有管理的企业");
            }
        }
        else{
            companyCore = companyCoreService.getById(companyId);
        }
        List<CompanyTags> result= companyTagsService.companyTagListFront(companyCore,member, CompanyTagApplyStatus.审核通过);
        return AjaxResult.success(result);
   }

    /**
     * 分页查询企业可以使用的标签 原来的  listTagType
     * @return
     */
    @GetMapping("listUseableCompanyTagCore")
    public AjaxResult<List<CompanyTagsCore>> listUseableCompanyTagCore() {
        List<CompanyTagsCore> result= companyTagsCoreService.listUseableCompanyTagsCore();
        return AjaxResult.success(result);
    }



    /**
     * 企业新增或者更新标签
     * @param companyTagUpdateDTO 新增或者修改企业的标签
     * @return
     */
    @PostMapping("saveCompanyTags")
    public AjaxResult<?> saveCompanyTags(@Validated @RequestBody CompanyTagUpdateDTO companyTagUpdateDTO) {
        companyTagsCoreService.saveCompanyTags(companyTagUpdateDTO,this.getLoginUser());
        return AjaxResult.success();
    }

    /**
     * 获取作为查询参数的标签
     * @return
     */
    @GetMapping("getCompanyTagsQueryParam")
    public AjaxResult<CompanyTagCoreQueryParamVO> getCompanyTagsQueryParam() {
        CompanyTagCoreQueryParamVO result = companyTagsService.getCompanyTagsQueryParam(this.getLoginUser());
        return AjaxResult.success(result);
    }



}
